package com.tanhua.dubbo.server.api;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;


@Service(version = "1.0.0")
public class RecommendUserApiImpl implements RecommendUserApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private UserLikeApi userLikeApi;

    @Override
    public RecommendUser queryWithMaxScore(Long userId) {
        //查询得分最高的用户，按照得分倒序排序
        Query query = Query.query(Criteria.where("toUserId").is(userId)).with(Sort.by(Sort.Order.desc("score"))).limit(1);
        return mongoTemplate.findOne(query, RecommendUser.class);
    }

    @Override
    public PageInfo<RecommendUser> queryPageInfo(Long userId, Integer pageNum, Integer pageSize) {
        //分页并且排序参数
        PageRequest pageRequest = PageRequest.of(pageNum - 1, pageSize, Sort.by(Sort.Order.desc("score")));
        //查询参数
        Query query = Query.query(Criteria.where("toUserId").is(userId)).with(pageRequest);

        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);
        //暂时不提供数据总数
        return new PageInfo<RecommendUser>(0, pageNum, pageSize, recommendUserList);
    }

    @Override
    public Double queryScore(Long userId, Long toUserId) {
        Query query = Query.query(Criteria.where("toUserId").is(toUserId)
                .and("userId").is(userId));
        RecommendUser recommendUser = this.mongoTemplate.findOne(query, RecommendUser.class);
        if (null != recommendUser) {
            return recommendUser.getScore();
        }
        return null;
    }

    /**
     * 查询探花列表，查询时需要排除喜欢与不喜欢的列表用户
     *
     * @param userId
     * @param count
     * @return
     */
    @Override
    public List<RecommendUser> queryCardList(Long userId, Integer count) {
        //设置分页,按照指定的查询条数和得分倒序排序
        PageRequest pageRequest = PageRequest.of(0, count, Sort.by(Sort.Order.desc("score")));
        //喜欢与不喜欢的用户列表
        List<Long> excludeList = new ArrayList<>();
        //查询喜欢的用户
        List<Long> likeList = userLikeApi.queryLikeList(userId);
        excludeList.addAll(likeList);
        //查询不喜欢的用户
        List<Long> notLikeList = userLikeApi.queryNotLikeList(userId);
        excludeList.addAll(notLikeList);
        //创建筛选条件
        Criteria criteria = Criteria.where("toUserId").is(userId);
        if (CollUtil.isNotEmpty(excludeList)) {
            //如果排除列表不为空,就添加排除条件
            criteria.andOperator(Criteria.where("userId").nin(excludeList));
        }
        Query query = Query.query(criteria).with(pageRequest);
        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);
        return recommendUserList;
    }
}
